<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="桌号ID" prop="tableId">
        <el-input v-model="queryParams.tableId" placeholder="请输入桌号ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否结账" prop="check">
        <el-input v-model="queryParams.check" placeholder="请输入结账 0 未结账 1 已结账" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否共享" prop="share">
        <el-input v-model="queryParams.share" placeholder="请输入共享 0  否  1 是" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="优惠卷" prop="coupon">
        <el-input v-model="queryParams.coupon" placeholder="请输入优惠卷 0 否 1 是" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['shop:cart:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['shop:cart:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['shop:cart:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['shop:cart:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cartList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="购物车ID" width="180" align="center" prop="cartId" />
      <!--    <el-table-column label="店家ID" align="center" prop="shopId" /> -->
      <el-table-column label="桌号ID" align="center" prop="tableId">
        <template slot-scope="{row}">
          <span style="color:#72E3E3;" v-if="row.tableId=='0'">无</span>
          <span style="color:#72E3E3;" v-if="row.tableId!='0'">{{row.tableId}}</span>
        </template>
      </el-table-column>
      <el-table-column label="商户订单号" align="center" prop="mchOrderNo" />

      <el-table-column label="食物菜单" align="center">
        <template slot-scope="scope">
          <el-popover align="center" :title="'购物车ID:'+scope.row.cartId" placement="top-start" width="800"
            trigger="click" @show="getCartId(scope.row.cartId)">
            <div>

              <el-table max-height="400" :data="foodList" border fit highlight-current-row
                style="width: 100%;">
                <el-table-column label="食物图片" align="center" width="100">
                  <template slot-scope="{row}">
                    <img width="40" height="40" :src="baseUrl+row.foodUrl" />
                  </template>
                </el-table-column>
                <el-table-column label="食物名称" align="center" width="200">
                  <template slot-scope="{row}">
                    <span>{{ row.foodName }}</span>
                  </template>
                </el-table-column>
                <el-table-column label="食物价格" width="100px" align="center">
                  <template slot-scope="{row}">
                    <span>￥{{ row.price }}</span>
                  </template>
                </el-table-column>
                <el-table-column label="数量" width="100px" align="center">
                  <template slot-scope="{row}">
                    <span>{{ row.count }} 份</span>
                  </template>
                </el-table-column>
                <el-table-column label="是否出餐" width="100px" align="center">
                  <template slot-scope="{row}">
                    <span style="color:#FF0000;" v-if="row.make==0">未出餐</span>
                    <span style="color:#E86C6C;" v-if="row.make==1">已出餐</span>
                  </template>
                </el-table-column>
                <el-table-column label="操作" width="150px" align="center">
                  <template slot-scope="{row}">
                    <el-button type="success" size="mini" @click="getFood1(row)" icon="el-icon-check" circle plain>
                    </el-button>
                    <el-button type="danger" size="mini" @click="getFood2(row)" icon="el-icon-close" circle plain>
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>
            <el-button slot="reference" type="text" plain>查看</el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="总消费金额(￥)" align="center" prop="amount" />
      <el-table-column label="优惠卷" align="center" prop="discountAmount" />
      <el-table-column label="类型" align="center" prop="cartType">
        <template slot-scope="{row}">
          <span style="color:#FA7A5B;" v-if="row.cartType==0">扫码点餐</span>
          <span style="color:#FA7A5B;" v-if="row.cartType==1">自提</span>
          <span style="color:#FA7A5B;" v-if="row.cartType==2">外卖</span>
          <span style="color:#FA7A5B;" v-if="row.cartType==3">预约</span>
          <span style="color:#FA7A5B;" v-if="row.cartType==4">其他</span>
        </template>
      </el-table-column>
      <el-table-column label="是否结账" align="center" prop="check">
        <template slot-scope="{row}">
          <span style="color:#E86C6C;" v-if="row.check==false">未结账</span>
          <span style="color:#6CB6E8;" v-if="row.check==true">已结账</span>
        </template>
      </el-table-column>
      <el-table-column label="是否共享" align="center" prop="share">
        <template slot-scope="{row}">
          <span style="color:#E86C6C;" v-if="row.share==false">否</span>
          <span style="color:#6CB6E8;" v-if="row.share==true">是</span>
        </template>
      </el-table-column>
      <el-table-column label="是否优惠" align="center" prop="coupon">
        <template slot-scope="{row}">
          <span style="color:#E86C6C;" v-if="row.coupon==false">否</span>
          <span style="color:#6CB6E8;" v-if="row.coupon==true">是</span>
        </template>
      </el-table-column>

      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['shop:cart:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['shop:cart:remove']">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改购物车信息对话框 -->
    <el-dialog :title="title" :visible.sync="open"  append-to-body center>
      <el-form style="width:80%; margin-left:10%;" label-position="top" class="demo" ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="店家ID" prop="shopId">
          <el-input v-model="form.shopId" placeholder="请输入店家ID" />
        </el-form-item>
        <el-form-item label="桌号ID" prop="tableId">
          <el-input v-model="form.tableId" placeholder="请输入桌号ID" />
        </el-form-item>
        <el-form-item label="商户订单号" prop="mchOrderNo">
          <el-input v-model="form.mchOrderNo" placeholder="请输入商户订单号" />
        </el-form-item>
        <el-form-item label="结账" prop="check">
          <el-input v-model="form.check" placeholder="请输入结账 0 未结账 1 已结账" />
        </el-form-item>
        <el-form-item label="共享" prop="share">
          <el-input v-model="form.share" placeholder="请输入共享 0  否  1 是" />
        </el-form-item>
        <el-form-item label="优惠卷" prop="coupon">
          <el-input v-model="form.coupon" placeholder="请输入优惠卷 0 否 1 是" />
        </el-form-item>
        <el-form-item label="食物菜单信息列表" prop="foodMenu">
          <el-input v-model="form.foodMenu" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="优惠卷" prop="discountAmount">
          <el-input v-model="form.discountAmount" placeholder="请输入优惠卷" />
        </el-form-item>
        <el-form-item label="总消费金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入总消费金额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listCart,
    getCart,
    delCart,
    addCart,
    updateCart
  } from "@/api/shop/cart";
  import store from '@/store'
  export default {
    name: "Cart",
    data() {
      return {
        cartId: 0,
        foodList:[],
        baseUrl: process.env.VUE_APP_BASE_IMAGE_URL,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 购物车信息表格数据
        cartList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          shopId: store.getters.shopId,
          tableId: null,
          mchOrderNo: null,
          check: null,
          share: null,
          coupon: null,
          foodMenu: null,
          discountAmount: null,
          amount: null,
          cartType: 1,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          check: [{
            required: true,
            message: "结账 0 未结账 1 已结账不能为空",
            trigger: "blur"
          }],
          share: [{
            required: true,
            message: "共享 0  否  1 是不能为空",
            trigger: "blur"
          }],
          coupon: [{
            required: true,
            message: "优惠卷 0 否 1 是不能为空",
            trigger: "blur"
          }],
          discountAmount: [{
            required: true,
            message: "优惠卷不能为空",
            trigger: "blur"
          }],
          amount: [{
            required: true,
            message: "总消费金额不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      getCartId(cartId) {
        console.log("cartId", cartId)
        this.cartId = cartId;
        getCart(cartId).then(response => {
          this.form = response.data;
          this.foodList=JSON.parse(response.data.foodMenu);
        });
      },
      getFood1(row) {
        console.log("row", row)
        this.foodList = JSON.parse(this.form.foodMenu)
        console.log("4444444444444444")
        for (let i = 0; i <  this.foodList.length; i++) {
          if ( this.foodList[i].foodId == row.foodId) {
             this.foodList[i].make = 1
          }
        }
        console.log("foodList:",this.foodList)
        this.form.foodMenu = JSON.stringify(this.foodList)
        updateCart(this.form).then(response => {
          this.$modal.msgSuccess("勾选成功");
          this.getList();
        });

      },
      getFood2(row) {
        console.log("row", row)
         this.foodList = JSON.parse(this.form.foodMenu)
        for (let i = 0; i <  this.foodList.length; i++) {
          if ( this.foodList[i].foodId == row.foodId) {
             this.foodList[i].make = 0
          }
        }
        this.form.foodMenu = JSON.stringify(this.foodList)
        updateCart(this.form).then(response => {
          this.$modal.msgSuccess("取消成功");
          this.getList();
        });
      },
      /** 查询购物车信息列表 */
      getList() {
        this.loading = true;
        listCart(this.queryParams).then(response => {
          this.cartList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          cartId: null,
          shopId: null,
          tableId: null,
          mchOrderNo: null,
          check: null,
          share: null,
          coupon: null,
          foodMenu: null,
          remark: null,
          discountAmount: null,
          amount: null,
          createTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.cartId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加购物车信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const cartId = row.cartId || this.ids
        getCart(cartId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改购物车信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.cartId != null) {
              updateCart(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addCart(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const cartIds = row.cartId || this.ids;
        this.$modal.confirm('是否确认删除购物车信息编号为"' + cartIds + '"的数据项？').then(function() {
          return delCart(cartIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('shop/cart/export', {
          ...this.queryParams
        }, `cart_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>


<style>
  .demo::-webkit-scrollbar {

    display: none;
    /* Chrome Safari */

  }



  .demo {

    scrollbar-width: none;
    /* firefox */

    -ms-overflow-style: none;
    /* IE 10+ */

    overflow-x: hidden;

    overflow-y: auto;
    height: 400px;
  }
</style>
